﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Models;
using MySql.Data.MySqlClient;
using Common;

namespace DAL
{
   public  class HingeDAL
    {
       //全部
       public DataTable HingeListAll(string hname) {
           string sql = string.Empty;
           if (!string.IsNullOrWhiteSpace(hname))
           {
               sql += " and hname like '%" + hname + "%'";
           }
           return SqlHelper.ExecuteDataTable("select * from HingeInfo where isdel =0 " + sql + " order by edittime desc");
       }
       //转换成MOdel
       public Hinge ToHinge(DataRow dr) {
           Hinge h = new Hinge();
           h.Hbrand = dr["hbrand"].ToString();
           h.Id = Convert.ToInt32(dr["id"]);
           h.Hname = dr["hname"].ToString();
           h.Hprice = Convert.ToDecimal(dr["Hprice"]);
           return h;
       }
       //根据id查询
       public Hinge GetHingeById(string id) {
           DataTable dt = SqlHelper.ExecuteDataTable("select * from hingeInfo where isdel=0 and id=@id",new MySqlParameter("@id",id));
           if (dt.Rows.Count == 1)
           {
               return ToHinge(dt.Rows[0]);
           }
           else {
               throw new Exception("存在重复数据");
           }
       }
       //更新
       public int UpdateData(Hinge h) {
           return SqlHelper.ExecuteNonQuery("update hingeInfo set hname=@hname,hbrand = @hbrand,hprice=@hprice,edittime=@edittime,edituser=@edituser where id=@id",
               new MySqlParameter("@hname", h.Hname), new MySqlParameter("@hbrand", h.Hbrand), new MySqlParameter("@hprice", h.Hprice), new MySqlParameter("@edittime", DateTime.Now.ToString()), 
               new MySqlParameter("@edituser", RemeberName.Username),new MySqlParameter("@id",h.Id));
       }
       //保存
       public int SaveData(Hinge h) {
           return SqlHelper.ExecuteNonQuery("insert into hingeInfo (hname,hprice,hbrand,isdel,addtime,edittime,adduser,edituser) values(@hname,@hprice,@hbrand,@isdel,@addtime,@edittime,@adduser,@edituser)",
               new MySqlParameter("@hname", h.Hname), new MySqlParameter("@hprice", h.Hprice), new MySqlParameter("@hbrand", h.Hbrand), new MySqlParameter("@isdel", h.Isdel), new MySqlParameter("@addtime", DateTime.Now.ToString()),
               new MySqlParameter("@edittime", DateTime.Now.ToString()), new MySqlParameter("@adduser", RemeberName.Username), new MySqlParameter("@edituser", RemeberName.Username));
       }
       //删除
       public int DelById(string id) {
           return SqlHelper.ExecuteNonQuery("update hingeInfo set isdel=1 ,edittime=@edittime,edituser=@edituser where id=@id",
               new MySqlParameter("@edittime",DateTime.Now.ToString()),new MySqlParameter("@edituser",RemeberName.Username),new MySqlParameter("@id",id));;
       }
    }
}
